﻿using FileClean.Language;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;


namespace FileClean
{
    public partial class Form1 : Form
    {
        private LanguageManager languageManager;
        private ScanDir scanDir;
        private TreeNodeCollection _selectedNode;
        private ContextMenuStrip _contextMenuStrip1;
        private ListViewItem _selectedListViewItem;
        public Form1()
        {
            InitializeComponent();
            languageManager = new LanguageManager();            
            this.Text = languageManager.GetString("test");
            string rootDirectory = @"L:\"; // 设置您要扫描的根目录
            
            // 设置右键菜单
            _contextMenuStrip1 = new ContextMenuStrip();
            ToolStripMenuItem deleteItem = new ToolStripMenuItem(languageManager.GetString("delete"));
            deleteItem.Click += DeleteItem_Click;
            _contextMenuStrip1.Items.Add(deleteItem);
        }
        private void BuildDirTree(string rootDirectory, TreeNodeCollection nodes)
        {
            // 显示扫描用时
            double utime = 0;
            // 显示扫描用时

            // 遍历目录
            Task.Run(() =>
            {
                Console.WriteLine("current thread id:" + Thread.CurrentThread.ManagedThreadId);
                // 开始扫描
                scanDir = new ScanDir(treeView1, listView1, rootDirectory);
                treeView1.Invoke((MethodInvoker)delegate () {
                    treeView1.Nodes.Clear();
                    listView1.Items.Clear();
                });
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                scanDir.ScanDirectory(rootDirectory, treeView1.Nodes);                
                stopwatch.Stop();
                // 显示扫描结果
                utime = stopwatch.Elapsed.TotalSeconds;
                Console.WriteLine(utime.ToString("F2") + "s");                
            }).ContinueWith(t =>
                scanUsedTime.Invoke((MethodInvoker)delegate ()
                {
                    scanUsedTime.Text = languageManager.GetString("scan_used_time") + utime.ToString("F2") + "s";
                })
             );
        }
        private void LoadDisks()
        {
            // 显示磁盘列表
            string[] disks = Environment.GetLogicalDrives();
            foreach (string disk in disks)
            {
                diskCmx.Items.Add(disk);
            }
        }
        private void DeleteItem_Click(object sender, EventArgs e)
        {
            // 删除选中的文件
            ToolStripMenuItem deleteItem = sender as ToolStripMenuItem;
            if(deleteItem!= null)
            {
                string nodeText = _selectedListViewItem.Text;
                // 显示确认对话框
                DialogResult result = MessageBox.Show(languageManager.GetString("confirm_delete") + nodeText, languageManager.GetString("confirm"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                {
                    return;
                }
                
                // 显示正在删除提示
                deleteFileLabel.Text = languageManager.GetString("deleting") + nodeText;    

                // 调用删除方法
                scanDir.DeleteSelectedNodes(_selectedNode, nodeText);

                scanDir.ShowNodes(_selectedNode, GetSelectedUnit());
            }            
        }
        private Common.FileSizeUnit GetSelectedUnit()
        {
            Common.FileSizeUnit unit = Common.FileSizeUnit.Byte;
            if (unitCmx.SelectedIndex == 0)
            {
                unit = Common.FileSizeUnit.Byte;
            }
            else if (unitCmx.SelectedIndex == 1)
            {
                unit = Common.FileSizeUnit.KB;
            }
            else if (unitCmx.SelectedIndex == 2)
            {
                unit = Common.FileSizeUnit.MB;
            }
            else if (unitCmx.SelectedIndex == 3)
            {
                unit = Common.FileSizeUnit.GB;
            }else if (unitCmx.SelectedIndex == 4)
            {
                unit = Common.FileSizeUnit.TB;
            }
            return unit;
        }
        private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // 获取被点击的节点
            TreeNodeCollection selectedNode = e.Node.Nodes;
            // 保存被点击的节点
            _selectedNode = selectedNode;
            // 排序
            scanDir.SortNodes(selectedNode);
            // 显示被点击的节点下的所有文件
            scanDir.ShowNodes(selectedNode,GetSelectedUnit());            
        }

        private void FileSizeUnitChange(object sender, EventArgs e)
        {
            scanDir.ShowNodes(_selectedNode, GetSelectedUnit());
        }

        private void ItemSelectChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {

            }
        }

        private void ListViewMouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                ListViewItem item = listView1.GetItemAt(e.X, e.Y);
                if(item != null)
                {
                    listView1.SelectedItems.Clear();
                    item.Selected = true;
                    _selectedListViewItem = item;
                    _contextMenuStrip1.Show(Cursor.Position);
                }
            }
        }

        private void DiskChange(object sender, EventArgs e)
        {
            string rootDirectory = diskCmx.Text;
            BuildDirTree(rootDirectory, treeView1.Nodes);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDisks();
        }
    }
}
